package com.epalmpay.service.bonus.impl;

import com.epalmpay.dto.bonus.*;
import com.epalmpay.entity.*;
import com.epalmpay.enumdef.ActivityEnum;
import com.epalmpay.mapper.*;
import com.epalmpay.service.bonus.IBonusSendShopService;
import com.epalmpay.util.DateUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by gwn on 2017/9/15.
 */
@Service("bonusSendShopService")
public class BonusSendShopServiceImpl implements IBonusSendShopService{

    @Resource
    private BonusSendShopMapper bonusSendShopMapper;

    @Resource
    private BonusVerifyShopMapper bonusVerifyShopMapper;
    @Resource
    private BonusSendShopMonthMapper bonusSendShopMonthMapper;
    @Resource
    private BonusSendLogMapper bonusSendLogMapper;
    @Resource
    private StatisGroupMapper statisGroupMapper;
    @Resource
    private BonusVerifyLogMapper bonusVerifyLogMapper;

    @Override
    public int queryBonusSendShopCount(BonusSendShopInDTO bonusSendShopInDTO) throws Exception {
        return bonusSendShopMapper.queryBonusSendShopCount(bonusSendShopInDTO);
    }

    @Override
    public int queryBonusVerifyShopCount(BonusSendShopInDTO bonusSendShopInDTO) throws Exception {
        return bonusVerifyShopMapper.queryBonusVerifyShopCount(bonusSendShopInDTO);
    }

    @Override
    public BonusSendShopListOutDTO queryBonusSendShopList(BonusSendShopInDTO bonusSendShopInDTO,Page page) throws Exception {

        BonusSendShopListOutDTO bonusSendShopListOutDTO = new BonusSendShopListOutDTO();

        Map<String,Object> param=new HashMap<String,Object>();
        param.put("groupId",bonusSendShopInDTO.getGroupId());
        param.put("shopName",bonusSendShopInDTO.getShopName());
        param.put("industryId",bonusSendShopInDTO.getIndustryId());
        if(bonusSendShopInDTO.getStatisDate()!=null){
            param.put("statisDate", bonusSendShopInDTO.getStatisDate()+"-01");
        }


//        int sumCount = bonusSendShopMapper.queryBonusSendShopDetailCount(param);
//        int sumCount =bonusSendLogMapper.queryBonusSendCount(param);
        List<BonusSendShopOutDTO> result =bonusSendLogMapper.queryBonusSendCount(param);
        int sumCount=result.size();
        page.setTotalResult(sumCount);
        if(sumCount<=0)
            return bonusSendShopListOutDTO;

        param.put("limit", page.getShowCount());
        param.put("start", page.getShowCount()*(page.getCurrentPage()-1));

//        List<BonusSendShopOutDTO> bonusSendShopOutDTOList = bonusSendShopMapper.queryBonusSendShopDetailList(param);
//        for (BonusSendShopOutDTO bonusSendShopOutDTO:bonusSendShopOutDTOList){
//            bonusSendShopOutDTO.setStatisDate(bonusSendShopOutDTO.getStatisDate().substring(0,4)+'-'+bonusSendShopOutDTO.getStatisDate().substring(4,6));
//        }
//        bonusSendShopListOutDTO.setBonusSendShopOutDTOList(bonusSendShopOutDTOList);
        List<BonusSendShopOutDTO> bonusSendShopOutDTOList=bonusSendLogMapper.findBonusSendShopLogList(param);
        bonusSendShopListOutDTO.setBonusSendShopOutDTOList(bonusSendShopOutDTOList);


        return bonusSendShopListOutDTO;
    }

    @Override
    public BonusSendShopOutDTO queryBonusSendShopDetail(BonusSendShopInDTO bonusSendShopInDTO) throws Exception {
        return bonusSendShopMapper.queryBonusSendShopDetail(bonusSendShopInDTO);
    }

    @Override
    public BonusSendShopListOutDTO queryBonusSendMonthList(BonusSendShopInDTO bonusSendShopInDTO, Page page) throws Exception {
        BonusSendShopListOutDTO bonusSendShopListOutDTO = new BonusSendShopListOutDTO();
        Map<String,Object> param=new HashMap<String,Object>();
        param.put("groupId",bonusSendShopInDTO.getGroupId());
        param.put("shopId",bonusSendShopInDTO.getShopId());


        int sumCount = bonusSendShopMonthMapper.queryBonusSendShopMonthDetailCount(param);
        page.setTotalResult(sumCount);
        if(sumCount<=0)
            return bonusSendShopListOutDTO;

        param.put("limit", page.getShowCount());
        param.put("start", page.getShowCount()*(page.getCurrentPage()-1));

        List<BonusSendShopMonthOutDTO> bonusSendShopMonthOutDTOList = bonusSendShopMonthMapper.queryBonusSendShopMonthDetailList(param);
        for (BonusSendShopMonthOutDTO bonusSendShopMonthOutDTO:bonusSendShopMonthOutDTOList){
            bonusSendShopMonthOutDTO.setStatisDate(bonusSendShopMonthOutDTO.getStatisDate().substring(0,4)+'-'+bonusSendShopMonthOutDTO.getStatisDate().substring(4,6));
        }
        bonusSendShopListOutDTO.setBonusSendShopMonthOutDTOList(bonusSendShopMonthOutDTOList);
        return bonusSendShopListOutDTO;
    }

    @Override
    public BonusSendShopListOutDTO queryBonusSendShopMonthList(BonusSendShopInDTO bonusSendShopInDTO, Page page) {
        BonusSendShopListOutDTO bonusSendShopListOutDTO = new BonusSendShopListOutDTO();
        Map<String,Object> param=new HashMap<String,Object>();
        param.put("groupId",bonusSendShopInDTO.getGroupId());
        param.put("gmtCreate",bonusSendShopInDTO.getJoinStart());
        param.put("shopId",bonusSendShopInDTO.getShopId());
        param.put("adjustStatus", ActivityEnum.BonusSendStatus.Accounted.getType());

        int sumCount = bonusSendLogMapper.findBonusSendLogCount(param);
        page.setTotalResult(sumCount);
        if(sumCount<=0)
            return bonusSendShopListOutDTO;

        param.put("limit", page.getShowCount());
        param.put("start", page.getShowCount()*(page.getCurrentPage()-1));

        List<BonusSendShopLogOutDTO> bonusSendLogOutDTOList = bonusSendLogMapper.findBonusSendLogList(param);
        bonusSendShopListOutDTO.setBonusSendShopLogOutDTOList(bonusSendLogOutDTOList);

        return bonusSendShopListOutDTO;

//        int sumCount = bonusSendShopMonthMapper.queryBonusSendShopMonthDetailCount(param);
//        page.setTotalResult(sumCount);
//        if(sumCount<=0)
//            return bonusSendShopListOutDTO;
//
//        param.put("limit", page.getShowCount());
//        param.put("start", page.getShowCount()*(page.getCurrentPage()-1));
//
//        List<BonusSendShopMonthOutDTO> bonusSendShopMonthOutDTOList = bonusSendShopMonthMapper.queryBonusSendShopMonthDetailList(param);
//        bonusSendShopListOutDTO.setBonusSendShopMonthOutDTOList(bonusSendShopMonthOutDTOList);
//        return bonusSendShopListOutDTO;
    }

    @Override
    public BonusSendShopMonth queryBonusSendShopMonthDetail(BonusSendShopInDTO bonusSendShopInDTO) {
        return bonusSendShopMonthMapper.queryBonusSendShopMonthDetail(bonusSendShopInDTO);
    }

    @Override
    public BonusSendShopListOutDTO bonusSendLogList(BonusSendShopInDTO bonusSendShopInDTO, Page page) {
        BonusSendShopListOutDTO bonusSendShopListOutDTO = new BonusSendShopListOutDTO();
        Map<String,Object> param=new HashMap<String,Object>();
        param.put("groupId",bonusSendShopInDTO.getGroupId());
        param.put("shopId",bonusSendShopInDTO.getShopId());
        param.put("gmtCreate",bonusSendShopInDTO.getStatisDate()+"-00");


        int sumCount = bonusSendLogMapper.queryMonthToBonusSendLogListCount(param);
        page.setTotalResult(sumCount);
        if(sumCount<=0)
            return bonusSendShopListOutDTO;

        param.put("limit", page.getShowCount());
        param.put("start", page.getShowCount()*(page.getCurrentPage()-1));

        List<BonusSendLogDetailOutDTO> bonusSendLogOutDTOList = bonusSendLogMapper.queryMonthToBonusSendLogList(param);
        bonusSendShopListOutDTO.setBonusSendLogOutDTOList(bonusSendLogOutDTOList);
        return bonusSendShopListOutDTO;
    }

    @Override
    public StatisGroup queryBonusCount(BonusSendShopInDTO bonusSendShopInDTO) throws Exception {
        return statisGroupMapper.queryBonusCount(bonusSendShopInDTO);
    }

    @Override
    public BonusSendLog bonusSendBonusAmount(Long groupId) {
        return bonusSendLogMapper.querySendBonusAmount(groupId);
    }

    @Override
    public BonusVerifyLog bonusVerifyAmount(Long groupId) {
        return bonusVerifyLogMapper.bonusVerifyAmount(groupId);
    }

    @Override
    public BonusSendShopListOutDTO bonusSendShopLogList(BonusSendShopInDTO bonusSendShopInDTO, Page page) {
        BonusSendShopListOutDTO bonusSendShopListOutDTO = new BonusSendShopListOutDTO();
        Map<String,Object> param=new HashMap<String,Object>();
        param.put("groupId",bonusSendShopInDTO.getGroupId());
        param.put("shopId",bonusSendShopInDTO.getId());
        param.put("joinStart",bonusSendShopInDTO.getJoinStart());
        param.put("joinEnd",bonusSendShopInDTO.getJoinEnd());


        int sumCount = bonusSendLogMapper.bonusSendShopLogListCount(param);
        page.setTotalResult(sumCount);
        if(sumCount<=0)
            return bonusSendShopListOutDTO;

        param.put("limit", page.getShowCount());
        param.put("start", page.getShowCount()*(page.getCurrentPage()-1));

        List<BonusSendLogDetailOutDTO> bonusSendLogOutDTOList = bonusSendLogMapper.bonusSendShopLogList(param);
        bonusSendShopListOutDTO.setBonusSendLogOutDTOList(bonusSendLogOutDTOList);
        return bonusSendShopListOutDTO;
    }

//    @Override
//    public BonusSendShopListOutDTO queryBonusVerifyShopList(BonusSendShopInDTO bonusSendShopInDTO,Page page) throws Exception {
//
//        BonusSendShopListOutDTO bonusSendShopListOutDTO = new BonusSendShopListOutDTO();
//
//        Map<String,Object> param=new HashMap<String,Object>();
//        param.put("groupId",bonusSendShopInDTO.getGroupId());
//
//
//
//        int sumCount = bonusVerifyShopMapper.queryBonusVerifyShopDetailCount(param);
//        page.setTotalResult(sumCount);
//        if(sumCount<=0)
//            return bonusSendShopListOutDTO;
//
//        param.put("limit", page.getShowCount());
//        param.put("start", page.getShowCount()*(page.getCurrentPage()-1));
//
//        List<BonusSendShopOutDTO> bonusSendShopOutDTOList = bonusVerifyShopMapper.queryBonusSendShopDetailList(param);
//        bonusSendShopListOutDTO.setBonusSendShopOutDTOList(bonusSendShopOutDTOList);
//        return bonusSendShopListOutDTO;
//    }



}
